#include "stdafx.h"
#include "Validator.h"
#include "EventArgs.h"
#include "CombatCard.h"
#include "ValidatorSkill.h"
#include "ValidatorObj.h"
#include "ValidatorEach.h"
#include "ValidatorContext.h"
#include "AppSetting.h"
#include "RandomStastic.h"
struct ValidatorDefReflectionRegister
{
	ValidatorDefReflectionRegister()
	{
		REGISTER_REFLECTION_MEMBER(Validator,m_iID,0).Editor_SetAsPrimaryKey();
		REGISTER_REFLECTION_MEMBER(Validator,m_sDisplayName,std::string("DefaultName")).Editor_SetAsDisplayName();
		REGISTER_REFLECTION_MEMBER(Validator,m_fRate,double(1.f)).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(Validator,m_iVSkill,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_ValidatorSkill);
		REGISTER_REFLECTION_MEMBER(Validator,m_iVOwner,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_ValidatorObj);
		REGISTER_REFLECTION_MEMBER(Validator,m_iVOther,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_ValidatorObj);
		REGISTER_REFLECTION_MEMBER(Validator,m_iVSrc,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_ValidatorObj);
		REGISTER_REFLECTION_MEMBER(Validator,m_iVEach,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_ValidatorEach);
		REGISTER_REFLECTION_MEMBER(Validator,m_iVContext,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_ValidatorContext);
		REGISTER_REFLECTION_MEMBER(Validator,m_tiOwnerAsSender,eTriple_Null).Editor_SetAsEnumation<ETriple>();
		REGISTER_REFLECTION_MEMBER(Validator,m_tiOtherAsSender,eTriple_Null).Editor_SetAsEnumation<ETriple>();

		GlobalClassManager::Instance().RegisterClass(Validator::s_GetReflection(),eDefaultDef_Validators);
		GlobalDefManager::Instance().RegisterDefManger(&ValidatorManager::Instance());

	}
}___validatordefreflectionregister;

bool Validator::Validate(Reactor * pReactor, EventArgBase& rEventArg ) const 
{
	if(!ValidateRate(pReactor))
	{
		return false;
	}
	if(!ValidateContext(rEventArg.m_pOwner->GetPlayerID(),rEventArg.m_pContext))
	{
		return false;
	}
	if(!ValidateSkill(rEventArg.m_pReactor))
	{
		return false;
	}
	if(!ValidateOwner(rEventArg.m_pOwner))
	{
		return false;
	}
	if(!ValidateOther(rEventArg.m_pOther))
	{
		return false;
	}
	if(!ValidateSrc(rEventArg.m_pSrc))
	{
		return false;
	}

	if(!ValidateEach(rEventArg.m_pOwner,rEventArg.m_pOther))
	{
		return false;
	}

	if(m_tiOwnerAsSender!=eTriple_Null)
	{
		if(rEventArg.m_pOwner->GetID()!=pReactor->GetSenderID()) return false;
	}
	if(m_tiOtherAsSender!=eTriple_Null)
	{
		if(rEventArg.m_pOther->GetID()!=pReactor->GetSenderID()) return false;
	}
	return true;
}
bool Validator::ValidateRate(Reactor * pReactor) const
{
	if(m_fRate<=0||m_fRate>=1) return true;

	float fRate=rand()%1000*1.f/1000.f;
	bool bOK= fRate<=m_fRate;
	if(AppSetting::Instance().IsEditor())
	{
		const ReactorDef *pDef=pReactor->GetDef();
		if(NULL!=pDef)
		{
			RandomStastics::Instance().Credit(pDef->m_sDisplayName,bOK);
		}
	}
	return bOK;
}

bool Validator::ValidateContext( uint8 iPlayerID,CombatSessionImp * pContext ) const
{
	if(!m_pContextValidators) return true;

	return m_pContextValidators->Validate(iPlayerID,pContext);
}

bool Validator::ValidateOwner( CombatObj * pObj ) const
{
	if(!m_pObjValidatorsOwner) return true;

	return m_pObjValidatorsOwner->Validate(pObj);
}
bool Validator::ValidateOther( CombatObj * pObj ) const
{
	if(!m_pObjValidatorsOther) return true;

	return m_pObjValidatorsOther->Validate(pObj);
}
bool Validator::ValidateSrc( CombatObj * pObj ) const
{
	if(!m_pObjValidatorsSrc) return true;

	return m_pObjValidatorsSrc->Validate(pObj);
}
bool Validator::ValidateEach( CombatObj * pOwner,CombatObj * pTarget ) const
{
	if(!m_pEachValidators) return true;
	return m_pEachValidators->Validate(pOwner,pTarget);
}

bool Validator::ValidateSkill(const Reactor * pRecator ) const 
{
	if(!m_pSkillValidators) return true;
	if(!pRecator) return true;

	return m_pSkillValidators->Validate(pRecator);
}

rewrite  void Validator::OnArchieveReaded()
{
	m_pSkillValidators=ValidatorSkillManager::Instance().LookupItem(m_iVSkill);
	m_pObjValidatorsOwner=ValidatorObjManager::Instance().LookupItem(m_iVOwner);
	m_pObjValidatorsOther=ValidatorObjManager::Instance().LookupItem(m_iVOther);
	m_pObjValidatorsSrc=ValidatorObjManager::Instance().LookupItem(m_iVSrc);
	m_pEachValidators=ValidatorEachManager::Instance().LookupItem(m_iVEach);
	m_pContextValidators=ValidatorContextManager::Instance().LookupItem(m_iVContext);
}